/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Servlet.tuan;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import server.entity.Photo;
import server.entity.RegisteredUser;
import server.process.ProcessBeanLocal;
import server.process.TopCommentPhoto;

/**
 *
 * @author Hoang
 */
public class controlertuan extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("action");
            if(action.equals("Update"))
            {
                String oldPass = request.getParameter("OldPassword");
                String newPass = request.getParameter("NewPassword");
                String ConfirmPassword = request.getParameter("ConfirmPassword");
                String name = request.getParameter("username");
                if(newPass.equals(ConfirmPassword))
                {
                    Context context = new InitialContext();
                    Object obj = context.lookup("ProcessLocalJNDI");
                    ProcessBeanLocal local = (ProcessBeanLocal) obj;
                    boolean ktChangePassAdmin = local.ChangePassWordAdmin(name, oldPass, newPass);
                    if(ktChangePassAdmin)
                    {
                        request.getRequestDispatcher("homeadmin.jsp").forward(request, response);
                    }
                    else
                    {
                        request.setAttribute("eros","loi nhap lai.");
                    request.getRequestDispatcher("changepasswordAdmin.jsp").forward(request, response);
                    }
                }
                else
                {
                    request.setAttribute("eros","loi nhap lai.");
                    request.getRequestDispatcher("changepasswordAdmin.jsp").forward(request, response);
                }
            }
            else if(action.equals("Login"))
            {
                String name = request.getParameter("txt1");
                String pass = request.getParameter("txt2");
                Context context = new InitialContext();
                Object obj = context.lookup("ProcessLocalJNDI");
                ProcessBeanLocal local = (ProcessBeanLocal) obj;
                boolean ktlogin = local.login(name, pass);
                if(ktlogin)
                {
                    HttpSession sesson = request.getSession();
                    local.LogAdminlogin();
                    sesson.setAttribute("admin", name);
                    response.sendRedirect("homeadmin.jsp");
                }
                else
                {
                    RequestDispatcher rd =request.getRequestDispatcher("loginAdmin.jsp");
                    request.setAttribute("erossLoginadmin", "Username or pass was wrong, please try agian");
                    rd.forward(request, response);
                }
            }
            else if(action.equals("changePasswordUser"))
            {
                String oldPass = request.getParameter("OldPassword");
                String newPass = request.getParameter("NewPassword");
                String ConfirmPassword = request.getParameter("ConfirmPassword");
                String name = request.getParameter("username");
                if(newPass.equals(ConfirmPassword))
                {
                    Context context = new InitialContext();
                    Object obj = context.lookup("ProcessLocalJNDI");
                    ProcessBeanLocal local = (ProcessBeanLocal) obj;
                    boolean ktChangePassuser = local.ChangePassUser(name, oldPass, newPass);
                    if(ktChangePassuser)
                    {
                        request.getRequestDispatcher("index.jsp").forward(request, response);
                    }
                    else
                    {
                        request.setAttribute("eros","loi nhap lai.");
                    request.getRequestDispatcher("changePassword.jsp").forward(request, response);
                    }
                }
                else
                {
                    request.setAttribute("eros","loi nhap lai.");
                    request.getRequestDispatcher("changePassword.jsp").forward(request, response);
                }
            }
            else if(action.equals("searchUser"))
            {
                String username = request.getParameter("txtsearch");
                Context context = new InitialContext();
                Object obj = context.lookup("ProcessLocalJNDI");
                ProcessBeanLocal local = (ProcessBeanLocal) obj;
                List<RegisteredUser> userSearch = local.SearchUserByUserName(username);

                request.setAttribute("UserSearch", userSearch);
                request.getRequestDispatcher("ManagerUser.jsp").forward(request, response);
            }
            else if(action.equals("ActiveUser"))
            {
                String username = request.getParameter("username");
                Context context = new InitialContext();
                Object obj = context.lookup("ProcessLocalJNDI");
                ProcessBeanLocal local = (ProcessBeanLocal) obj;
                local.ActiveUser(username);
                response.sendRedirect("homeadmin.jsp");
            }
            else if(action.equals("LogUser"))
            {
                String username = request.getParameter("username");
                Context context = new InitialContext();
                Object obj = context.lookup("ProcessLocalJNDI");
                ProcessBeanLocal local = (ProcessBeanLocal) obj;
                local.LogUser(username);
                response.sendRedirect("homeadmin.jsp");
            }
            else if(action.equals("myprofile"))
            {
                String name = request.getParameter("username");
                Context context = new InitialContext();
                Object obj = context.lookup("ProcessLocalJNDI");
                ProcessBeanLocal local = (ProcessBeanLocal) obj;
                RegisteredUser user = local.OneUser(name);
                request.setAttribute("oneUser", user);
                request.getRequestDispatcher("myprofile.jsp").forward(request, response);
            }
            else if(action.equals("UpdateProfile"))
            {
                String username = request.getParameter("Username");
                String firstname = request.getParameter("firstName");
                String lastname = request.getParameter("LastName");
                String email = request.getParameter("email");
                String phone = request.getParameter("PhoneNo");
                String address = request.getParameter("Address");
                Context context = new InitialContext();
                Object obj = context.lookup("ProcessLocalJNDI");
                ProcessBeanLocal local = (ProcessBeanLocal) obj;
                boolean ktupdanteUser = local.UserUpdate(username, firstname, lastname, address, phone, email);
                if(ktupdanteUser)
                {
                    response.sendRedirect("home.jsp");
                }
            }
            else if(action.equals("ReportTuan"))
            {
                String fromDate = request.getParameter("fromDate");
                String toDate = request.getParameter("toDate");
                Context context = new InitialContext();
                Object obj = context.lookup("ProcessLocalJNDI");
                ProcessBeanLocal local = (ProcessBeanLocal) obj;
                List<TopCommentPhoto> lists = local.reportCommentPhoto(fromDate, toDate);
                if(lists.isEmpty())
                {
                    request.setAttribute("eross","null");
                request.getRequestDispatcher("ReportComment.jsp").forward(request, response);
                }
                request.setAttribute("report",lists);
                request.getRequestDispatcher("ReportComment.jsp").forward(request, response);
            }
            else if(action.equals("logoutAdmin"))
            {
                HttpSession session = request.getSession();
                session.removeAttribute("admin");
                response.sendRedirect("loginAdmin.jsp");
            }
            else if(action.equals("createCategory"))
            {
               String name = request.getParameter("name");
               String dec = request.getParameter("dec");
               String image = request.getParameter("image");
               HttpSession season = request.getSession();
               String username = season.getAttribute("username").toString();
               boolean addmin = false;
               Context context = new InitialContext();
               Object obj = context.lookup("ProcessLocalJNDI");
               ProcessBeanLocal local = (ProcessBeanLocal) obj;
               if(local.createNewCollection(name, dec, username, addmin, image))
               {
                    out.print("success");
               }
            }
            else if(action.equals("managerImage"))
            {
                String id = request.getParameter("id");
                Context context = new InitialContext();
                Object obj = context.lookup("ProcessLocalJNDI");
                ProcessBeanLocal local = (ProcessBeanLocal) obj;
                List<Photo> list = local.listphotobyCategoryID(id);
                System.out.println(list.size());
                for (int i= 0; i < list.size(); i++) {
                    System.out.println(list.get(i).getPhotoName());
                }
               request.setAttribute("list", list);
               request.getRequestDispatcher("manageImage.jsp").forward(request, response);
            }
            else if(action.equals("logImage"))
            {
                String id = request.getParameter("id");
                Context context = new InitialContext();
                Object obj = context.lookup("ProcessLocalJNDI");
                ProcessBeanLocal local = (ProcessBeanLocal) obj;
                boolean kt = local.logStatusPhoto(id);
                if(kt)
                {
                    response.sendRedirect("ManagerCollection.jsp");
                }
            }
            else if(action.equals("activeImage"))
            {
                String id = request.getParameter("id");
                Context context = new InitialContext();
                Object obj = context.lookup("ProcessLocalJNDI");
                ProcessBeanLocal local = (ProcessBeanLocal) obj;
                boolean kt = local.activeStatusPhoto(id);
                if(kt)
                {
                    response.sendRedirect("ManagerCollection.jsp");
                }
            }
            else if(action.equals("viewPhotoinCategory"))
            {
                String id = request.getParameter("id");
                Context context = new InitialContext();
                Object obj = context.lookup("ProcessLocalJNDI");
                ProcessBeanLocal local = (ProcessBeanLocal) obj;
                List<Photo> list = local.listphotobyCategoryID(id);
                request.setAttribute("list", list);
               request.getRequestDispatcher("ImageinCategory.jsp").forward(request, response);
            }
            else if(action.equals("getListPhoto"))
            {
                Context context = new InitialContext();
                Object obj = context.lookup("ProcessLocalJNDI");
                ProcessBeanLocal local = (ProcessBeanLocal) obj;
                List<Photo> lstPhoto = local.getListPhoto();
                request.setAttribute("lstPhoto",lstPhoto);
                request.getRequestDispatcher("allimage.jsp").forward(request, response);

            }


        }catch(Exception ex)
        {
            ex.printStackTrace();
        }

        finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
